package com.kcs.common.utils;

import java.io.Serializable;
import java.util.*;

public class PropertySet implements Serializable {

    public PropertySet()
    {
        type = "";
        value = "";
        property = new Properties();
        child = new Vector();
        enumPropertyNames = null;
    }

    public int addChild(PropertySet psChild)
    {
        child.addElement(psChild);
        return child.size() - 1;
    }

    public PropertySet getChild(int idxChild)
    {
        return (PropertySet)child.get(idxChild);
    }

    public PropertySet[] getChildren(String type_value)
    {
        Enumeration child_enum = child.elements();
        Vector matched_child = new Vector();
        do
        {
            if(!child_enum.hasMoreElements())
                break;
            PropertySet propset = (PropertySet)child_enum.nextElement();
            String propset_type = propset.getType();
            if(propset_type != null && propset_type.equals(type_value))
                matched_child.addElement(propset);
        } while(true);
        PropertySet propsets[] = new PropertySet[matched_child.size()];
        for(int i = 0; i < matched_child.size(); i++)
            propsets[i] = (PropertySet)matched_child.elementAt(i);

        return propsets;
    }

    public int getChildCount()
    {
        return child.size();
    }

    public void removeChild(int idxChild)
    {
        child.remove(idxChild);
    }

    public String getFirstProperty()
    {
        enumPropertyNames = property.propertyNames();
        if(enumPropertyNames.hasMoreElements())
            return (String)enumPropertyNames.nextElement();
        else
            return null;
    }

    public String getNextProperty()
    {
        if(enumPropertyNames == null)
            return null;
        if(enumPropertyNames.hasMoreElements())
            return (String)enumPropertyNames.nextElement();
        else
            return null;
    }

    public String getProperty(String pKey)
    {
        return property.getProperty(pKey);
    }

    public String getType()
    {
        return type;
    }

    public String getValue()
    {
        return value;
    }

    public void insertChildAt(PropertySet psChild, int idxChild)
    {
        child.insertElementAt(psChild, idxChild);
    }

    public boolean propertyExist(String pKey)
    {
        return property.containsKey(pKey);
    }

    public void removeProperty(String pKey)
    {
        property.remove(pKey);
    }

    public void reset()
    {
        property = new Properties();
        child = new Vector();
        type = "";
        value = "";
        enumPropertyNames = null;
    }

    public void setProperty(String pKey, String pVal)
    {
        property.setProperty(pKey, pVal);
    }

    public void setType(String t)
    {
        type = t;
    }

    public void setValue(String v)
    {
        value = v;
    }

    protected String type;
    protected String value;
    protected Properties property;
    protected Vector child;
    protected transient Enumeration enumPropertyNames;
}
